package Main;

import java.util.Scanner;
import model.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 初始化
        Teacher[] teachers = {
                new Teacher("张老师"), new Teacher("李老师"), new Teacher("王老师")
        };

        Course[] courses = {
                new Course("数学", "教学楼101", "周一 8:00"),
                new Course("英语", "教学楼102", "周三 10:00"),
                new Course("Java编程", "机房201", "周五 14:00")
        };

        Student student = new Student("小明");

        System.out.println("=== 学生选课系统 ===");

        while (true) {
            System.out.println("\n1.教师开课 2.学生选课 3.学生退课 4.学生课表 5.教师课表 6.退出");
            System.out.print("选择: ");

            int choice = getValidInput(scanner, 6);

            switch (choice) {
                case 1: offerCourse(scanner, teachers, courses); break;
                case 2: selectCourse(scanner, student, courses); break;
                case 3: student.dropCourse(); break;
                case 4: student.printSchedule(); break;
                case 5: showTeacherSchedule(scanner, teachers); break;
                case 6:
                    System.out.println("再见！");
                    scanner.close();
                    return;
                default: System.out.println("无效选择");
            }
        }
    }

    // 验证输入是否在有效范围内的通用方法
    private static int getValidInput(Scanner scanner, int max) {
        while (true) {
            try {
                int input = scanner.nextInt();
                if (input >= 1 && input <= max) {
                    return input;
                } else {
                    System.out.println("输入无效，请输入 " + 1 + " 到 " + max + " 之间的数字:");
                }
            } catch (Exception e) {
                System.out.println("输入无效，请输入数字:");
                scanner.next(); // 清除无效输入
            }
        }
    }

    private static void offerCourse(Scanner scanner, Teacher[] teachers, Course[] courses) {
        System.out.print("选择教师(1.张老师 2.李老师 3.王老师): ");
        int teacherIndex = getValidInput(scanner, teachers.length) - 1;
        Teacher teacher = teachers[teacherIndex];

        System.out.print("选择课程(1.数学 2.英语 3.Java): ");
        int courseIndex = getValidInput(scanner, courses.length) - 1;
        Course course = courses[courseIndex];

        teacher.offerCourse(course);
    }

    private static void selectCourse(Scanner scanner, Student student, Course[] courses) {
        System.out.print("选择课程(1.数学 2.英语 3.Java): ");
        int courseIndex = getValidInput(scanner, courses.length) - 1;
        student.selectCourse(courses[courseIndex]);
    }

    private static void showTeacherSchedule(Scanner scanner, Teacher[] teachers) {
        System.out.print("选择教师(1.张老师 2.李老师 3.王老师): ");
        int teacherIndex = getValidInput(scanner, teachers.length) - 1;
        teachers[teacherIndex].printSchedule();
    }
}